package com.ruoyi.house_sale.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.house_sale.domain.*;
import com.ruoyi.house_sale.service.IHouseinfoService;
import com.ruoyi.house_sale.service.ISalesGuideService;
import com.ruoyi.house_sale.service.ISalestateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房产资料Controller
 *
 * @author ruoyi
 * @date 2022-03-30
 */
@RestController
@RequestMapping("/house_sale/salesguide")
public class SalesGuideController extends BaseController
{
    @Autowired
    private IHouseinfoService houseInfoService;
    @Autowired
    private ISalesGuideService salesGuideService;
    @Autowired
    private ISalestateService salestateService;

    /**
     * 查询房产资料列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:salesGuide:list')")
    @GetMapping("/list")
    public TableDataInfo list(Terminfo terminfo)
    {
        long termid = terminfo.getTermid();
        List<SalesGuide> list = new ArrayList<>();
        if(termid > 0) {
            list = salesGuideService.getSalesGuideList(termid);
        }
        if(list.size() < 1) {
            SalesGuide s = new SalesGuide();
            s.setHouseinfolist(new ArrayList<>());
            list.add(s);
        }
        return getDataTable(list);
    }

    /**
     * 获取房产资料详细信息
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:query')")
    @GetMapping(value = "/{hourseid}")
    public AjaxResult getInfo(@PathVariable("hourseid") Long hourseid)
    {

        return AjaxResult.success(houseInfoService.selectHouseinfoByHourseid(hourseid));
    }

    /**
     * 查询项目列表
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:list')")
    @GetMapping("/projectList")
    public TableDataInfo projectList()
    {
        List<ProjectInfo> list = salesGuideService.getProjectInfoList();
        return getDataTable(list);
    }

    /**
     * 查询楼栋列表
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:list')")
    @GetMapping("/termList")
    public TableDataInfo termList(Terminfo terminfo)
    {
        long projectid = terminfo.getProjectid();
        List<Terminfo> list = new ArrayList<>();
        if(projectid > 0) {
            list = salesGuideService.getTermInfoList(projectid);
        }
        return getDataTable(list);
    }

    /**
     * 初始化
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:list')")
    @GetMapping("/init")
    public Map init()
    {
        List<ProjectInfo> projectList = salesGuideService.getProjectInfoList();
        long projectid = projectList.get(0).getProjectid();

        List<Terminfo> termList = salesGuideService.getTermInfoList(projectid);
        long termid = termList.get(0).getTermid();

        List<SalesGuide> salesGuideList = salesGuideService.getSalesGuideList(termid);

        Map map = new HashMap();
        map.put("projectinfolist", projectList);
        map.put("terminfolist", termList);
        map.put("HouseList", salesGuideList);

        return map;
    }


    /**
     * 销售状态
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:list')")
    @GetMapping("/saleStateList")
    public AjaxResult saleStateList()
    {
        List<Salestate> salestates = salestateService.selectSalestateList(new Salestate());
        return AjaxResult.success(salestates);
    }

    /**
     * 修改销售状态
     */
//    @PreAuthorize("@ss.hasPermi('house_sale:HouseInfo:list')")
    @PostMapping("/updateSaleState")
    public AjaxResult updateSaleState(Houseinfo houseinfo)
    {
        int count = salesGuideService.updateSaleState(houseinfo);
        return AjaxResult.success(count);
    }

}
